#region Copyright Jonathan Quiros

//	
// All rights are reserved. Reproduction or transmission in whole or in 
// part, in any form or by any means, electronic, mechanical or otherwise, 
// is prohibited without the prior written consent of the copyright owner.
//
// Filename: TournamentRepository.cs
//

#endregion

using System;
using System.Linq;
using System.Collections.Generic;
using Ids.Web.Listing;
using Novagoal.Models;
using Novagoal.Models.Utils;
using Novagoal.Repositories.Extensions;

namespace Novagoal.Repositories.Entities
{
    public class TournamentRepository : BaseRepository, ITournamentsRepository
    {
        #region CONSTANTS



        #endregion

        #region PROPERTIES

        

        #endregion

        #region PUBLIC METHODS

        public List<Tournament> GetTournaments(Dictionary<TournamentListFilter, string> filterList, TournamentListSortField sortField, SortDirectionType sortDirection, int currentPage, int pageSize)
        {
            var qryTournaments = FilterTournaments(filterList);

            var teams = SortTeams(qryTournaments, sortField, sortDirection)
                .TakePage(currentPage, pageSize)
                .AsEnumerable()
                .Select(CreateTournament)
                .ToList();

            return teams;
        }

        public int GetTotalTournaments(Dictionary<TournamentListFilter, string> filterList)
        {
            return FilterTournaments(filterList).Count();
        }

        public Tournament GetTournament(int id)
        {
            return EntitiesContext.tournaments
                .Where(t => t.id == id)
                .Select(CreateTournament)
                .FirstOrDefault();
        }

        public IEnumerable<Tournament> GetTournamentsByPerson(int personId)
        {
            return
                EntitiesContext.parties_people.Where(pp => pp.people.id == personId && pp.parties.tournaments.enabled)
                    .Select(pp => pp.parties.tournaments)
                    .Distinct()
                    .Select(CreateTournament)
                    .OrderBy(t => t.Name)
                    .ToList();
        }

        public IEnumerable<Tournament> GetTournaments()
        {
            return EntitiesContext.tournaments
                .Select(CreateTournament)
                .OrderBy(t => t.Name)
                .ToList();
        }

        #endregion

        #region PRIVATE METHODS

        private IQueryable<tournaments> FilterTournaments(Dictionary<TournamentListFilter, string> filters)
        {
            var tournaments = EntitiesContext.tournaments as IQueryable<tournaments>;

            if (filters != null)
            {
                if (filters.Keys.Contains(TournamentListFilter.Id))
                {
                    int id;
                    if (int.TryParse(filters[TournamentListFilter.Id], out id))
                    {
                        return tournaments.Where(item => item.id == id);
                    }
                }
                if (filters.Keys.Contains(TournamentListFilter.Name))
                {
                    var strName = filters[TournamentListFilter.Name];
                    tournaments = tournaments.Where(item => item.name.Contains(strName));
                }
            }

            return tournaments;
        }

        private IQueryable<tournaments> SortTeams(IQueryable<tournaments> query, TournamentListSortField sortField, SortDirectionType sortDirection)
        {
            switch (sortField)
            {
                case TournamentListSortField.Id:
                    return query.OrderBy(item => item.id, sortDirection);

                case TournamentListSortField.Name:
                    return query.OrderBy(item => item.name, sortDirection);

                case TournamentListSortField.CreatedAt:
                    return query.OrderBy(item => item.created_at, sortDirection);

                case TournamentListSortField.UpdatedAt:
                    return query.OrderBy(item => item.updated_at, sortDirection);

            }
            return query.OrderByDescending(item => item.id);
        }

        #endregion
    }
}